package cn.glutnn.service.impl;

import cn.glutnn.entity.Bank;
import cn.glutnn.entity.Result;
import cn.glutnn.entity.dto.LoginDTO;
import cn.glutnn.entity.dto.PasswordDTO;
import cn.glutnn.mapper.BankMapper;
import cn.glutnn.mapper.BankUserMapper;
import cn.glutnn.service.BankService;
import cn.glutnn.service.BankUserService;
import cn.glutnn.utils.JwtUtil;
import cn.glutnn.utils.Md5Util;
import cn.glutnn.utils.ThreadLocalUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import cn.glutnn.entity.BankUser;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * (BankUser)表服务实现类
 *
 * @author msh
 * @since 2024-10-27 00:37:52
 */
@Service
public class BankUserServiceImpl extends ServiceImpl<BankUserMapper, BankUser> implements BankUserService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private BankMapper bankMapper;

    @Override
    public Result<BankUser> updatePassword(PasswordDTO passwordDTO) {
        Map o = ThreadLocalUtil.get();
        String username = (String) o.get("username");
        BankUser user = getOne(new LambdaQueryWrapper<BankUser>().eq(BankUser::getUserName, username));

        if (!Md5Util.checkPassword(passwordDTO.getOldPassword(), user.getPassword())) {
            return Result.error("旧密码错误");
        }
        user.setPassword(Md5Util.getMD5String(passwordDTO.getNewPassword()));
        user.setUpdateTime(new Date());
        boolean update = update(user, new UpdateWrapper<BankUser>().eq("user_name", username));
        if (!update) {
            return Result.error("修改密码失败");
        }

        return Result.success();
    }


    @Override
    public Result login(LoginDTO loginDTO) {
        LambdaQueryWrapper<BankUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BankUser::getUserName, loginDTO.getUserName());
        BankUser user = getOne(wrapper);
        if (user == null) {
            return Result.error("用户不存在");
        }
        if (!Md5Util.checkPassword(loginDTO.getPassword(), user.getPassword())) {
            return Result.error("密码错误");
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("username", user.getUserName());
        map.put("bankId", user.getBankId());


        String token = JwtUtil.genToken(map);


        Bank bank = bankMapper.selectOne(new LambdaQueryWrapper<Bank>().eq(Bank::getBankId, user.getBankId()));

        Map<String, Object> rs = new HashMap<>();
        rs.put("token", token);
        rs.put("bankName", bank.getBankName());
        rs.put("bankId", user.getBankId());
        rs.forEach((k, v) -> System.out.println(k + ":" + v));
        // 将token存入redis
        ValueOperations<String, String> opsForValue = stringRedisTemplate.opsForValue();
        opsForValue.set("rxt_admin_Token", token);
        return Result.success(rs);
    }

    @Override
    public Result<BankUser> selectByName() {
        Map o = ThreadLocalUtil.get();
        Object o1 = o.get("username");
        BankUser user = getOne(new QueryWrapper<BankUser>().eq("user_name", o1));

        return Result.success(user);
    }

    @Override
    public Result<BankUser> updateByUsername(BankUser bankUser) {
        Map o = ThreadLocalUtil.get();
        Object o1 = o.get("username");
        bankUser.setUserName(o1.toString());
        bankUser.setUpdateTime(new Date());

        boolean update = update(bankUser, new QueryWrapper<BankUser>().eq("user_name", o1));
        if (!update) {
            return Result.error("修改失败");
        }
        return Result.success();
    }

    @Override
    public Result<BankUser> selectUserByName(String userName) {
        BankUser user = getOne(new QueryWrapper<BankUser>().eq("user_name", userName));

        return Result.success(user);
    }

    @Override
    public Result logout() {
        stringRedisTemplate.delete("rxt_admin_Token");
        System.out.println("退出成功");
        return Result.success("登出");
    }

}

